package com.fhd.dao.Bill;

import com.fhd.dao.BaseDao;
import com.fhd.pojo.Bill;
import com.fhd.pojo.Recharge;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class BillDaoImplement implements BillDao {
    // 创建订单
    @Override
    public int createBill(Connection conn, int clientId, int merchantId, int houseId,int money, Date startTime, Date endTime, int number) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "insert into bill (client_id,merchant_id,house_id,money,start_time,end_time,number) values (?,?,?,?,?,?,?)";
                Object[] parameters = {clientId,merchantId,houseId,money,startTime,endTime,number};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 获取某个用户的所有订单
    @Override
    public ArrayList<Bill> getClientBill(Connection conn, int clientId) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<Bill> bills = new ArrayList<>();

        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select b.*,c.user_name as user_name,m.shop_name as shop_name,h.house_name as house_name from bill b,client c,merchant m,house h where b.client_id = ? and b.client_id = c.id and b.merchant_id = m.id and b.house_id = h.id";
                Object[] parameters = {clientId};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);

                while (rs.next()) {
                    Bill bill = new Bill();
                    bill.setId(rs.getInt("id"));
                    bill.setMerchantId(rs.getInt("merchant_id"));
                    bill.setClientId(rs.getInt("client_id"));
                    bill.setHouseId(rs.getInt("house_id"));
                    bill.setStartTime(rs.getDate("start_time"));
                    bill.setEndTime(rs.getDate("end_time"));
                    bill.setSituation(rs.getInt("situation"));
                    bill.setNumber(rs.getInt("number"));
                    bill.setMoney(rs.getInt("money"));
                    bill.setClientUserName(rs.getString("user_name"));
                    bill.setMerchantShopName(rs.getString("shop_name"));
                    bill.setHouseName(rs.getString("house_name"));
                    bills.add(bill);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return bills;
    }

    // 更改订单状态
    @Override
    public int updateSituation(Connection conn, int id,int situation) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "update bill set situation = ? where id = ?";
                Object[] parameters = {situation,id};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 得到某个商户的所有订单
    @Override
    public ArrayList<Bill> getMerchantBill(Connection conn, int merchantId) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<Bill> bills = new ArrayList<>();

        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select b.*,c.user_name as user_name,m.shop_name as shop_name,h.house_name as house_name from bill b,client c,merchant m,house h where b.merchant_id = ? and b.client_id = c.id and b.merchant_id = m.id and b.house_id = h.id";
                Object[] parameters = {merchantId};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);

                while (rs.next()) {
                    Bill bill = new Bill();
                    bill.setId(rs.getInt("id"));
                    bill.setMerchantId(rs.getInt("merchant_id"));
                    bill.setClientId(rs.getInt("client_id"));
                    bill.setHouseId(rs.getInt("house_id"));
                    bill.setStartTime(rs.getDate("start_time"));
                    bill.setEndTime(rs.getDate("end_time"));
                    bill.setSituation(rs.getInt("situation"));
                    bill.setNumber(rs.getInt("number"));
                    bill.setMoney(rs.getInt("money"));
                    bill.setClientUserName(rs.getString("user_name"));
                    bill.setMerchantShopName(rs.getString("shop_name"));
                    bill.setHouseName(rs.getString("house_name"));
                    bills.add(bill);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return bills;
    }
}
